package com.happle.gcmclient.dialog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.DialogInterface;
import android.widget.ArrayAdapter;

import com.happle.gcmclient.BaseActivity;
import com.happle.gcmclient.R;
import com.happle.gcmclient.backendmanager.task.UpdateLanguageTask;
import com.happle.gcmclient.config.CommonUtilities;
import com.happle.gcmclient.config.GlobalValue;
import com.happle.gcmclient.managers.LngManager;
import com.happle.gcmclient.obj.LngObj;
import com.happle.gcmclient.utility.DialogUtility;

public class AddLanguageDialog  extends BaseActivity {

    protected UpdateLanguageTask updLngTask;
	private List<LngObj> listAllLanguages;
	private ArrayAdapter<String> lngAdapter;
	private LngManager lngManager;
	public String[] items;
	private Activity self;
	private SelectLanguageListener listener;

	public interface SelectLanguageListener {

		void onSelect(int index, LngObj languageObject,
				List<LngObj> listLanguages);

	}
    public interface CancelLanguageListener {
            void onCancel(DialogInterface dialog);
    }

	// Constructor function : Add params into this
	public AddLanguageDialog(Activity context, SelectLanguageListener listener) {
		this.self = context;
		this.listener = listener;
		listAllLanguages = new ArrayList<LngObj>();
		if (lngManager == null) {
			lngManager = new LngManager(self);
		}
		listAllLanguages = lngManager.getSelectedLanguages(false);
		items = new String[listAllLanguages.size()];
		int i = 0;
		for (LngObj item : listAllLanguages) {
			items[i] = item.get_lng_name();
			i++;
		}
	}

	public void show() {
		DialogUtility.showSimpleOptionDialogNeutralBtn(self,
				R.string.btn_add_lng, items,
				self.getString(R.string.btn_cancel), onLngItemClick);
	}

	DialogInterface.OnClickListener onLngItemClick = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			LngObj updateLng = listAllLanguages.get(which);
			updateLng.set_is_marked(true);
			if (lngManager == null) {
				lngManager = new LngManager(self);
			}
            updateLng.set_is_sent((updateLanguage(updateLng) == 1) ? true : false);
			for (int i = 0; i < 5; i++) {
                //update on server
				if (lngManager.updateLngSettings(updateLng)) {
					i = 5;
				}
			}
            listener.onSelect(which, updateLng, listAllLanguages);
		}
	};
    private int updateLanguage(LngObj lngObj) {
      
        int taskResult = -1;
        try {
            updLngTask = new UpdateLanguageTask(lngObj, GlobalValue.registerId);
            updLngTask.execute();
            taskResult = updLngTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        //taskResult = 1; // temp, remove when service is ready
        return taskResult;
    }
}
